package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * http://localhost:8083/admin/storage/list
 * ?page=1&limit=20&key=111&name=2222&sort=add_time&order=desc
 */
@RestController
@RequestMapping("/admin/storage")
@CrossOrigin
@Slf4j
public class StorageController {

    @Autowired
    private DtsStorageService dtsStorageService;
    /**
     * 分页查询
     */
    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu = "系统管理",button = "对象管理")
    @GetMapping("/list")
    public Object storageList(            Integer page,
                                          Integer limit,
                                          String name,
                                          String key,
                                          String sort,
                                          String order

    ){
        //MybatisPlus: 分页对象
        IPage<DtsStorage> ipage = dtsStorageService.selectPage(page,limit,name,key,sort,order);
        Map<String,Object> result = new HashMap<>();
        result.put("total",ipage.getTotal());
        result.put("items",ipage.getRecords());

        return ResponseUtil.ok(result);

    }
    /**
     * 上传  添加
     * {
     * 	"errno": 0,
     * 	"data": {
     * 		"id": 34,
     * 		"key": "dd75ec03-8f88-447b-a925-4e7f35a299f4.jpg",
     * 		"name": "16.jpg",
     * 		"type": ".jpg",
     * 		"size": 35012,
     * 		"url": "http://qianfeng-file.oss-cn-beijing.aliyuncs.com/dd75ec03-8f88-447b-a925-4e7f35a299f4.jpg?Expires=1622471984&OSSAccessKeyId=LTAI4GCjGC58aGgnLjd5hixp&Signature=%2Bu%2BczJdE4I6jW4DagLVYT0Ps3dw%3D",
     * 		"addTime": 1621391984382,
     * 		"updateTime": 1621391984382,
     * 		"deleted": false
     *        },
     * 	"errmsg": "成功"
     * }
     */
    @RequiresPermissions("admin:storage:create")
    @RequiresPermissionsDesc(menu = "系统管理",button = "对象添加")
    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception{//上传文件

        String originalFilename = file.getOriginalFilename();//上传时候的文件的名称 叫原始名称
        long size = file.getSize();
        byte[] fileBytes = file.getBytes();

        DtsStorage dtsStorage = dtsStorageService.create(originalFilename,size,fileBytes);
        return ResponseUtil.ok(dtsStorage);
    }

    @PostMapping("/delete")
    public Object delete(DtsStorage storage) {

        dtsStorageService.delete(storage);
        return ResponseUtil.ok();
    }
}
